package com.seventeen.monster.web.controller.proxy;


import com.seventeen.monster.common.annotation.Log;
import com.seventeen.monster.common.constant.DelFlagConstants;
import com.seventeen.monster.common.core.controller.BaseController;
import com.seventeen.monster.common.core.domain.AjaxResult;
import com.seventeen.monster.common.core.page.TableDataInfo;
import com.seventeen.monster.common.enums.BusinessType;
import com.seventeen.monster.mq.proxy.domain.MqProducerProxy;
import com.seventeen.monster.mq.proxy.service.IMqProducerProxyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 生产代理
 *
 * @author seventeen
 */
@RestController
@RequestMapping(value = "/mq/producer/proxy")
public class MqProducerProxyController extends BaseController {

    @Autowired
    private IMqProducerProxyService mqProducerProxyService;

    @PreAuthorize("@ss.hasPermi('mq:producer:list')")
    @GetMapping("/list")
    public TableDataInfo list(MqProducerProxy mqProducerProxy)
    {
        startPage();
        List<MqProducerProxy> list = mqProducerProxyService.queryList(mqProducerProxy);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('mq:producer:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult get(@PathVariable Long id)
    {
        MqProducerProxy mqProducerProxy = new MqProducerProxy();
        mqProducerProxy.setId(id);
        List<MqProducerProxy> list = mqProducerProxyService.queryList(mqProducerProxy);
        return success(CollectionUtils.isEmpty(list)?null:list.get(0));
    }

    @PreAuthorize("@ss.hasPermi('mq:producer:add')")
    @Log(title = "生产者管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MqProducerProxy mqProducerProxy)
    {
        mqProducerProxy.setCreateBy(getUsername());
        mqProducerProxyService.add(mqProducerProxy);
        return AjaxResult.success();
    }

    /**
     * 删除消费者
     */
    @PreAuthorize("@ss.hasPermi('mq:producer:remove')")
    @Log(title = "生产者管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        MqProducerProxy mqProducerProxy = new MqProducerProxy();
        mqProducerProxy.setId(id);
        mqProducerProxy.setDelFlag(DelFlagConstants.DELETED);
        mqProducerProxy.setUpdateBy(getUsername());
        mqProducerProxyService.update(mqProducerProxy);
        return success();
    }

}
